﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using VioletDevelopmentLaboratory.Net.Web;
using VioletDevelopmentLaboratory.Net.Web.Component;

public partial class vboard_membership_modify : VioletDevelopmentLaboratory.Net.Web.PageBase
{
	protected void Page_Init(object sender, EventArgs e)
	{
		Page.Header.Title = GetLocalResourceObject("Title").ToString();
	}

	protected void PasswordCheckButton_Click(object sender, EventArgs e)
	{
		if (Page.IsValid)
		{
			if (Membership.ValidateUser(Page.User.Identity.Name, PasswordTextBox.Text))
			{
				PasswordCheckPanel.Visible = false;
				ModifyPanel.Visible = !PasswordCheckPanel.Visible;

				LoadUserInfo();

				QuestionDropDownList_DataBind();
			}
			else
				VUtility.ShowMessagePanel(Page, VUtility.GetVWebGlobalResource("Membership_Do_CheckPassword_Failure"), true);
		}
	}

	private void LoadUserInfo()
	{
		MembershipUser user = Membership.GetUser();

		CreationDate.Text = user.CreationDate.ToString();
		LastLoginDate.Text = user.LastLoginDate.ToString();
		LastPasswordChangedDate.Text = user.LastPasswordChangedDate.ToString();

		EmailTextBox.Text = user.Email;
		ConfirmEmailTextBox.Text = user.Email;
		CommentTextBox.Text = user.Comment;
	}

	private void QuestionDropDownList_DataBind()
	{
		QuestionDropDownList.DataSource = VUtility.GetVWebGlobalResource("Membership_PasswordQuestions").Split(new string[] {"\r\n"}, StringSplitOptions.RemoveEmptyEntries);
		QuestionDropDownList.DataBind();
	}




	protected void BasicModifyButton_Click(object sender, EventArgs e)
	{
		if (Page.IsValid)
		{
			MembershipUser user = Membership.GetUser();
			user.Email = EmailTextBox.Text;
			user.Comment = CommentTextBox.Text;

			Membership.UpdateUser(user);

			VUtility.ShowMessagePanel(Page, VUtility.GetVWebGlobalResource("Membership_Do_ModifyBasic_Success"), true);

			LoadUserInfo();
		}
	}



	protected void PasswordModifyButton_Click(Object sender, EventArgs e)
	{
		if (Page.IsValid)
		{
			MembershipUser user = Membership.GetUser();

			if (user.ChangePassword(CurrentPasswordTextBox.Text, NewPasswordTextBox.Text))
				VUtility.ShowMessagePanel(Page, VUtility.GetVWebGlobalResource("Membership_Do_ModifyPassword_Success"), true);
			else
				VUtility.ShowMessagePanel(Page, VUtility.GetVWebGlobalResource("Membership_Do_ModifyPassword_Failure"), true);
		}
	}



	protected void SecurityQAModifyButton_Click(object sender, EventArgs e)
	{
		if (Page.IsValid)
		{
			MembershipUser user = Membership.GetUser(Page.User.Identity.Name);

			Boolean changeResult = user.ChangePasswordQuestionAndAnswer(
				CurrentPasswordTextBox2.Text,
				QuestionDropDownList.SelectedValue,
				NewAnswerTextBox.Text);

			if (changeResult)
			{
				VUtility.ShowMessagePanel(Page, VUtility.GetVWebGlobalResource("Membership_Do_ModifySecurityQA_Success"), true);
				QuestionDropDownList.SelectedIndex = -1;
				NewAnswerTextBox.Text = "";
			}
			else
				VUtility.ShowMessagePanel(Page, VUtility.GetVWebGlobalResource("Membership_Do_ModifySecurityQA_Failure"), true);
		}
	}
}
